<?php
/**
 * Admin utils
 *
 * @author Anton
 */
class Lemar_From
{
	
//----------------------------------------------------------------------------------------------------
	
	public static function saveMulti(array $data)
    {
    	if (count($data) > 0)
    	{
	    	foreach ($data as $d)
	    	{
		    	if ( self::validateFrom($d['from'], $d['id_campaign']) === true )
				{
					$result = models_CampaignfromMapper::save($d);
					$id_from[] = (int) $result;
				}
	    	}
	    	return $id_from;
    	}
    }
    
//----------------------------------------------------------------------------------------------------
	
	public static function validateFrom($from , $id_campaign)
    {
    	$from = trim($from);
    	$id_campaign = (int) $id_campaign;
    	$validatorEmail = new Zend_Validate_EmailAddress();
    	
		if ($from)
		{
			if ($validatorEmail->isValid($from))
			{
				// email appears to be valid
				if ( models_CampaignfromMapper::inCampaign($from, $id_campaign) )
				{
					return " From e-mail '".$from."' is already in Campaign (id: ".$id_campaign.").<br />";
				}
				else
				{
					// if 'From' is not in campaign, so it can be added.
					return true;
				}
			}
			else
			{
				// email is invalid
				$messages = '';
				foreach ($validatorEmail->getMessages() as $messageId => $message)
				{
					$messages .= " $message<br />";
				}
				return $messages." 'From' e-mail '".$from."' will not be added.";
			}
		}
		else
		{
			return false;
		}
    }
    
//----------------------------------------------------------------------------------------------------
    
}